﻿// CheckCaseEditDialog.cpp: 实现文件
//

#include "stdafx.h"
#include "zChecker.h"
#include "CheckCaseEditDialog.h"
#include "afxdialogex.h"


// CCheckCaseEditDialog 对话框

IMPLEMENT_DYNAMIC(CCheckCaseEditDialog, CDialogEx)

CCheckCaseEditDialog::CCheckCaseEditDialog(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_CHECK_CASE_EDIT, pParent)
{
	m_pCase = NULL;
}

CCheckCaseEditDialog::~CCheckCaseEditDialog()
{
	if (m_pCase)
	{
		m_pCase->Release();
		m_pCase = NULL;
	}
}

void CCheckCaseEditDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_TREE_CASE, m_caseTree);
	DDX_Control(pDX, IDC_TREE_OPERATOR, m_operTree);
	DDX_Control(pDX, IDC_MFCPROPERTYGRID1, m_ruleProps);
}


BEGIN_MESSAGE_MAP(CCheckCaseEditDialog, CDialogEx)
	ON_WM_SIZE()
	ON_NOTIFY(NM_CLICK, IDC_TREE_CASE, &CCheckCaseEditDialog::OnNMClickTreeCase)
	ON_NOTIFY(NM_CLICK, IDC_TREE_OPERATOR, &CCheckCaseEditDialog::OnNMClickTreeOperator)
END_MESSAGE_MAP()


// CCheckCaseEditDialog 消息处理程序


BOOL CCheckCaseEditDialog::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	m_caseTree.AttachBitmap();
	m_operTree.AttachBitmap();

	m_caseTree.SetCheckCase(m_pCase);
	m_caseTree.Populate();

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}


void CCheckCaseEditDialog::OnSize(UINT nType, int cx, int cy)
{
	CDialogEx::OnSize(nType, cx, cy);

	// TODO: 在此处添加消息处理程序代码
}

void CCheckCaseEditDialog::SetCheckCase(ck::CheckCase* pCase)
{
	if (m_pCase)
	{
		m_pCase->Release();
		m_pCase = NULL;
	}
	m_pCase = pCase;
	if (m_pCase)
	{
		m_pCase->AddRef();
	}
}

void CCheckCaseEditDialog::OnNMClickTreeCase(NMHDR *pNMHDR, LRESULT *pResult)
{
	// TODO: 在此添加控件通知处理程序代码
	CPoint pt;
	GetCursorPos(&pt);
	m_caseTree.ScreenToClient(&pt);

	UINT uFlag = 0;
	HTREEITEM hItem = m_caseTree.HitTest(pt, &uFlag);
	if ((NULL == hItem) || !(TVHT_ONITEM&uFlag))
	{
		return;
	}
	m_caseTree.SelectItem(hItem);
	int nNodeType = m_caseTree.GetNodeType(hItem);
	switch (nNodeType)
	{	
	case CASE_TREE_ITEM_NODE:
	{
		z_uint32 nCkItemCode = (z_uint32)m_caseTree.GetItemData(hItem);
		z_uint32 nModelCode = m_pCase->GetModeCode();

		ck::EnumOperator* pOperators = NULL;
		ck::CheckModelManager* pModelManager = ck::CheckModelManager::GetInstance();
		pOperators = pModelManager->GetOperators(nModelCode, nCkItemCode);
		m_operTree.Populate(pOperators);
		pOperators->Release();
	}
		break;
	}

	*pResult = 0;
}


void CCheckCaseEditDialog::OnNMClickTreeOperator(NMHDR *pNMHDR, LRESULT *pResult)
{
	// TODO: 在此添加控件通知处理程序代码
	CPoint pt;
	GetCursorPos(&pt);
	m_operTree.ScreenToClient(&pt);

	UINT uFlag = 0;
	HTREEITEM hItem = m_operTree.HitTest(pt, &uFlag);
	if ((NULL == hItem) || !(TVHT_ONITEM&uFlag))
	{
		return;
	}

	HTREEITEM hRoot = m_operTree.GetRootItem();
	HTREEITEM hParent = m_operTree.GetParentItem(hItem);
	if (hParent == NULL)
	{
		if (TVHT_ONITEMICON&uFlag)
		{//点击到根节点图标上

			BOOL checked = m_operTree.GetCheck(hItem);
			checked = !checked;

			int icon = checked ? OPER_TREE_CHECK_ICON : OPER_TREE_UNCHECK_ICON;
			m_operTree.SetItemImage(hItem, icon, icon);
			m_operTree.SetCheck(hItem, checked);
			//m_ruleProps.EnableWindow(checked);
		}

		if (TVHT_ONITEM&uFlag)
		{	
			z_uint32 nOperCode = m_operTree.GetOperatorCode(hItem);
			z_uint32 nCaseCode = m_pCase->GetCode();
			z_uint32 nItemCode = m_caseTree.GetCheckElementCode(m_caseTree.GetSelectedItem());
			ck::CheckCaseManager* pCaseManager = ck::CheckCaseManager::GetInstance();

			BOOL checked = m_operTree.GetCheck(hItem);
			//根据Check的情况进行处理
			if (checked)
			{	//如果check，说明要对该算子进行检查，则设置PropertyGrid
				//1）设置PropertyGrid为可用状态				
				//2）更新PropertyGrid的值

				//3）将该算子添加到CK_CASE_RULE表中
				pCaseManager->AddRule(nCaseCode, nItemCode, nOperCode);
			}
			else
			{
				//如果uncheck，说明要对该算子进行检查，则设置PropertyGrid
				//1）设置PropertyGrid为可用状态

				//2）更新PropertyGrid的值

				//3）将该算子从CK_CASE_OPERATOR表中删除
			}
		}
	}


	*pResult = 0;
}

